// pages/myAddressAddEdit/myAddressAddEdit.js
var app = getApp();
var c = require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['辽宁省', '沈阳市', '和平区'],
    customItem: '全部'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var address_id = options.address_id;
    var that = this;
    if (address_id) {
      c.ajax(
        app.url + 'toAddresssIdGetAddressInfo', {
          address_id: address_id
        },
        res => {
          var city = [res.data[0].province, res.data[0].city, res.data[0].area];
          that.setData({
            data: res.data[0],
            region: city,
          })
        }
      )
    } else {
      var  address_id= 0
    }
    var user_id = wx.getStorageSync('userInfo').user_id;
    this.setData({
      address_id: address_id,
      user_id: user_id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit: function(e) {
    var addressDetails = e.detail.value.addressDetails;
    var cityArray = e.detail.value.city;
    var phone = e.detail.value.phone;
    var userName = e.detail.value.userName;
    var user_id = this.data.user_id;
    var address_id = this.data.address_id;
    c.ajax(
      app.url + 'setAddress', {
        addressDetails: addressDetails,
        cityArray: cityArray,
        phone: phone,
        userName: userName,
        user_id: user_id,
        address_id: address_id
      },
      res => {
        if (res.data) {
          wx.showToast({
            title: '设置成功',
            icon: 'none'
          })
          setTimeout(function() {
            wx.navigateTo({
              url: '/pages/myaddr/myaddr',
            })
          }, 1000)
        }
      }
    )
  }
})